package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.ExportProductDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class PackingServiceImpl implements PackingService {
    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ContractDao contractDao;


    @Override
    public void save(Packing packing) {
        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setState("0");
        packingDao.insertSelective(packing);

    }

    @Override
    public Packing getPacking(String id) {
        Packing packing = new Packing();
        String[] arrayIds = id.split(",");
        List<String> idList = Arrays.asList(arrayIds);
        packing.setExportIds(id);
        ArrayList<Export> list = new ArrayList<>();
        //总体积
        Double totalVolume=0d;
        //总净重
        Double netWeights=0d;
        //总毛重
        Double grossWeights=0d;
        //合同号
        String contractNos="";
        //获取总净重总毛重总体积合同号
        if (idList!=null&&idList.size()>0){
            for (String exportId : idList) {
                //根据报运单id获取报运单商品对象
                ExportProductExample exportProductExample = new ExportProductExample();
                exportProductExample.createCriteria().andExportIdEqualTo(exportId);
                List<ExportProduct> exportProducts = exportProductDao.selectByExample(exportProductExample);
                if (exportProducts!=null&&exportProducts.size()>0) {
                    for (ExportProduct exportProduct : exportProducts) {
                        if (exportProduct!=null){
                            if (exportProduct.getNetWeight()!=null){
                                netWeights+=exportProduct.getNetWeight();
                            }
                            if (exportProduct.getGrossWeight()!=null){
                                grossWeights+=exportProduct.getGrossWeight();
                            }
                            if (exportProduct.getSizeHeight()!=null&&exportProduct.getSizeLength()!=null&&exportProduct.getSizeWidth()!=null){
                                totalVolume=(totalVolume+(exportProduct.getSizeHeight())*exportProduct.getSizeLength()*exportProduct.getSizeWidth());
                            }
                        }
                    }
                }
                //根据报运单id获取报运单对象
                Export export = exportDao.selectByPrimaryKey(exportId);
                list.add(export);
                String contractIds = export.getContractIds();
                String[] cIds = contractIds.split(",");
                List<String> cIdLilst = Arrays.asList(cIds);
                if (cIdLilst!=null&&cIdLilst.size()>0){
                    for (String contractId : cIdLilst) {
                        Contract contract = contractDao.selectByPrimaryKey(contractId);
                        if (contract!=null){
                            if (contract.getContractNo()!=null){
                                contractNos=contractNos+","+contract.getContractNo();
                            }
                        }

                    }
                }

            }
        }
        //收货人
        String consignee = list.get(0).getConsignee();
        //装货港
        String shipmentPort = list.get(0).getShipmentPort();
        //卸货港
        String destination = list.get(0).getDestinationPort();
        //判断三个属性是否统一
        for (int i = 0; i < list.size(); i++) {
            if (!(consignee.equals(list.get(i).getConsignee()))
                    ||!(shipmentPort.equals(list.get(i).getShipmentPort()))
                    ||!(destination.equals(list.get(i).getDestinationPort()))){
                return null;
            }
        }
        packing.setNetWeights(netWeights);
        packing.setGrossWeights(grossWeights);
        packing.setTotalVolume(totalVolume);
        packing.setExportNos(contractNos);
        packing.setPackingMoney(grossWeights*520);
        return packing;
    }

    @Override
    public PageInfo<Packing> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PackingExample packingExample = new PackingExample();
        packingExample.createCriteria();
        List<Packing> list = packingDao.selectByExample(packingExample);
        return new PageInfo<>(list);
    }

    @Override
    public String delete(String id) {
        Packing packing = packingDao.selectByPrimaryKey(id);
        String state = packing.getState();
        if (state.equals("0")){
            packingDao.deleteByPrimaryKey(id);
            return "1";
        }
        return "0";
    }

    @Override
    public boolean isPass(String id) {
        Packing packing = packingDao.selectByPrimaryKey(id);
        return packing.getState().equals("0");
    }

    @Override //id参数有可能是一个多个id组成的
    public boolean isShipment(String id) {
        String[] arrayIds = id.split(",");
        List<String> idList = Arrays.asList(arrayIds);
        if (idList!=null&&idList.size()>0){
            for (String exportId : idList) {
                Export export = exportDao.selectByPrimaryKey(exportId);
                if (export.getState()!=2){
                    return true;
                }

            }
        }
        return false;
    }
}
